package Chuong04;

import java.awt.Color;
import java.awt.GridLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JTextField;

public class Bai06 extends JFrame implements ActionListener{
	JButton btnMax = new JButton("Số lớn nhất");
	JButton btnMin = new JButton("Số nhỏ nhất");
	
	JTextField txtS1, txtS2, txtS3;
	
	public Bai06(){
		setSize(300, 180);
	    setTitle("Bài 06 - Chương 04");
	    btnMax.addActionListener(this);
	    btnMin.addActionListener(this);
	    
	    JPanel p = new JPanel();
	    p.setLayout(new GridLayout(4,2));
	    
	    txtS1 = new JTextField();
	    txtS2 = new JTextField();
	    txtS3 = new JTextField();
	    
	    p.add(new JLabel("Số thứ nhất:"));
	    p.add(txtS1);
	    p.add(new JLabel("Số thứ hai:"));
	    p.add(txtS2);
	    p.add(new JLabel("Số thứ ba:"));
	    p.add(txtS3);
	    
	    p.add(btnMax);
	    p.add(btnMin);
	    
	    add(p);
	}

	public static void main(String[] args) {
		Bai06 frm = new Bai06();
		frm.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
		frm.setVisible(true);
	}

	
	@Override
	public void actionPerformed(ActionEvent e) {
		if(e.getSource() == btnMax){
			int s1 = Integer.parseInt(txtS1.getText());
			int s2 = Integer.parseInt(txtS2.getText());
			int s3 = Integer.parseInt(txtS3.getText());
			if(s1 == s2 && s2 == s3){
				txtS1.setBackground(Color.YELLOW);
				txtS2.setBackground(Color.YELLOW);
				txtS3.setBackground(Color.YELLOW);
			}else if(s2 >= s1 && s2 >= s3){
				txtS2.setBackground(Color.RED);
				txtS1.setBackground(Color.WHITE);
				txtS3.setBackground(Color.WHITE);
			}else if(s3 >= s2 && s1 <= s3){
				txtS3.setBackground(Color.RED);
				txtS2.setBackground(Color.WHITE);
				txtS1.setBackground(Color.WHITE);
			}else if(s1 >= s2 && s1 >= s3){
				txtS1.setBackground(Color.RED);
				txtS2.setBackground(Color.WHITE);
				txtS3.setBackground(Color.WHITE);
			}
		}
		
		if(e.getSource() == btnMin){
			int s1 = Integer.parseInt(txtS1.getText());
			int s2 = Integer.parseInt(txtS2.getText());
			int s3 = Integer.parseInt(txtS3.getText());
			if(s1 == s2 && s2 == s3){
				txtS1.setBackground(Color.YELLOW);
				txtS2.setBackground(Color.YELLOW);
				txtS3.setBackground(Color.YELLOW);
			}else if(s1 <= s2 && s1 <= s3){
				txtS1.setBackground(Color.GREEN);
				txtS2.setBackground(Color.WHITE);
				txtS3.setBackground(Color.WHITE);
			}else if(s2 <= s1 && s2 <= s3){
				txtS2.setBackground(Color.GREEN);
				txtS1.setBackground(Color.WHITE);
				txtS3.setBackground(Color.WHITE);
			}else if(s3 <= s2 && s1 >= s3){
				txtS3.setBackground(Color.GREEN);
				txtS2.setBackground(Color.WHITE);
				txtS1.setBackground(Color.WHITE);
			}
		}
	}
}
